package com.zhaopin.test;

import java.io.*;

public class FileReadWriteDemo {
    public static void main(String[] args) {
        String inputFile = "dir/input.txt"; // 原始文件路径
        String outputFile = "dir/output.txt"; // 新建文件路径

        try (BufferedReader reader = new BufferedReader(new FileReader(inputFile));
             BufferedWriter writer = new BufferedWriter(new FileWriter(outputFile))) {

            String line;
            while ((line = reader.readLine()) != null) {
                // 输出到控制台
                System.out.println(line);

                // 写入新文件
                writer.write(line);
                writer.newLine(); // 添加换行符以保持与原文件相同的格式
            }

            System.out.println("Content from 'input.txt' has been printed to console and written to 'output.txt'.");

        } catch (IOException e) {
            System.err.println("An error occurred while reading/writing files:");
            e.printStackTrace();
        }
    }
}
